package com.hk.service.quartz;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.hk.commons.ConstantRoute;
import com.hk.commons.DateUtil;
import com.hk.commons.HqlFilter;
import com.hk.commons.LongId;
import com.hk.commons.enm.OrderCodeEnum;
import com.hk.commons.entity.order.OrderRouteInfo;
import com.hk.commons.entity.sys.SysUser;
import com.hk.commons.entity.yingmi.YingmiConfirmFile;
import com.hk.commons.utils.yingmi.YingmiApiUtils;
import com.hk.service.order.IOrderRouteInfo;
import com.hk.service.sys.ISysUser;
import com.hk.service.yingmi.IYingmiConfirmFile;

public class YingmiUpdateToRouteQuarze extends BaseQuartz {

	private Logger logger = LoggerFactory
			.getLogger(YingmiUpdateToRouteQuarze.class);

	@Autowired
	private YingmiApiUtils utils;

	@Autowired
	private IYingmiConfirmFile yingmiConfirmFile;

	@Autowired
	private ISysUser sysUserService;// 用户信息

	@Autowired
	private IOrderRouteInfo orderRouteInfoService;

	@Override
	public void start() {
		try {
			logger.info("更新盈米确认订单的定时任务开启");
	        ExecutorService executor = Executors.newFixedThreadPool(1);
	        Runnable task = () -> saveYingmiToRoute();
	        executor.execute(task);
	        executor.shutdown();
			logger.info("更新盈米确认订单的定时任务结束");
		} catch (Exception e) {
			logger.info("更新盈米确认订单异常", e);
		}

		// this.saveYingmiToRoute();
	}

	private void saveYingmiToRoute() {
		HqlFilter hqlFilter = new HqlFilter();

		List<YingmiConfirmFile> list = yingmiConfirmFile
				.findByFilter(hqlFilter); // 盈米确认订单查询
		// logger.info("list size:{}" , list.size());

		for (YingmiConfirmFile ycf : list) {
			OrderRouteInfo orderRouteInfo = new OrderRouteInfo();
			orderRouteInfo.setId(LongId.get());
			orderRouteInfo.setProductId(1L);
			orderRouteInfo.setAccountId(ycf.getAccountId());
			orderRouteInfo.setBrokerOrderNo(ycf.getBrokerOrderNo());
			orderRouteInfo.setCompanyId(ConstantRoute.COMPANY_YINGMI);
			// orderRouteInfo.setProductId(Long.valueOf(ycf.getOrderId()));
			orderRouteInfo.setCts(DateUtil.transferStringToLong(ycf.getCts()));
			Long orderId = Long.valueOf(ycf.getOrderId());

			orderRouteInfo.setOrderId(Long.valueOf(ycf.getOrderId()));

			Integer fundOrderCode = OrderCodeEnum.getOrderTypeByCode(ycf
					.getFundOrderCode());
			// Integer fundOrderCode=OrderCodeEnum.getOrderTypeByCode("weryi");
			orderRouteInfo.setStatus(fundOrderCode);

			// if("24".equals(ycf.getFundOrderCode())){//FUNDORDERCORDER_24("24",
			// "赎回"),
			// orderRouteInfo.setStatus(24);
			// }
			HqlFilter sysHqlFilter = new HqlFilter();
			sysHqlFilter.addEQFilter("accountId", ycf.getAccountId());// 从盈米每个订单中得到盈米的投资者的唯一账户ID，再到user用户表中查到这个人（user表里会有推荐码和推荐人）
			SysUser sysUser = null;
			List<SysUser> slist = sysUserService.findByFilter(sysHqlFilter);
			if (slist.size() != 0) {
				sysUser = slist.get(0);
				orderRouteInfo.setUserId(sysUser.getId());

				if (sysUser.getRecommendId() != null) {
					orderRouteInfo.setFinancialPlannerId(sysUser
							.getRecommendId());
				} else {
					if (StringUtils.isEmpty(sysUser.getRecommendCode())) {
						orderRouteInfo.setFinancialPlannerId(null);
					} else {
						HqlFilter uqlFilter = new HqlFilter();
						uqlFilter.addEQFilter("name",
								sysUser.getRecommendCode());// 推荐code就是手机号，而在数据库对应的手机号为name
						List<SysUser> ulist = sysUserService
								.findByFilter(uqlFilter);
						if (ulist.size() > 0) {
							SysUser user = ulist.get(0);
							orderRouteInfo.setFinancialPlannerId(user.getId());
						} else {
							orderRouteInfo.setFinancialPlannerId(null);
						}
					}
				}
			} else {

				orderRouteInfo.setFinancialPlannerId(null);
			}

			HqlFilter filter = new HqlFilter();
			filter.addEQFilter("orderId", orderId);
			List<OrderRouteInfo> routeList = orderRouteInfoService
					.findByFilter(filter);
			if (routeList.size() == 0) {
				orderRouteInfoService.add(orderRouteInfo);
			}

			// orderRouteInfoService.merge(orderRouteInfo);//订单路由表增加
		}
	}

}
